package com.doubao.notepad_blog_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.doubao.notepad_blog_backend.common.api.ApiResult;
import com.doubao.notepad_blog_backend.common.exception.ApiAsserts;
import com.doubao.notepad_blog_backend.domain.BmsFollow;
import com.doubao.notepad_blog_backend.domain.UmsUser;
import com.doubao.notepad_blog_backend.service.BmsFollowService;
import com.doubao.notepad_blog_backend.service.UmsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.doubao.notepad_blog_backend.jwt.JwtUtil.USER_NAME;

@RestController
@RequestMapping("/relationship")
@Api(value = "关注管理",tags = {"关注管理"})
public class BmsFollowController {

    @Resource
    private BmsFollowService bmsFollowService;

    @Resource
    private UmsUserService umsUserService;

    @ApiOperation( "添加关注" )
    @GetMapping("/subscribe/{userId}")
    public ApiResult<Object> handleFollow(
            @RequestHeader(value = USER_NAME) String userName
            , @PathVariable("userId") String parentId) {

        UmsUser umsUser = umsUserService.getOne( new LambdaQueryWrapper<UmsUser>().eq( UmsUser::getUsername,
                userName ) );

        if (parentId.equals( umsUser.getId() )) {
            ApiAsserts.fail( "怎么可以关注自己呢 😮" );
        }
        BmsFollow one = bmsFollowService.getOne(
                new LambdaQueryWrapper<BmsFollow>()
                        .eq( BmsFollow::getParentId, parentId )
                        .eq( BmsFollow::getFollowerId, umsUser.getId() ) );
        if (!ObjectUtils.isEmpty( one )) {
            ApiAsserts.fail( "已关注" );
        }

        BmsFollow follow = new BmsFollow();
        follow.setParentId( parentId );
        follow.setFollowerId( umsUser.getId() );
        bmsFollowService.save( follow );
        return ApiResult.success( null, "关注成功" );
    }

    @ApiOperation( "取消关注" )
    @GetMapping("/unsubscribe/{userId}")
    public ApiResult<Object> handleUnFollow(@RequestHeader(value = USER_NAME) String userName
            , @PathVariable("userId") String parentId) {

        UmsUser umsUser = umsUserService.getOne( new LambdaQueryWrapper<UmsUser>().eq( UmsUser::getUsername,
                userName ) );

        BmsFollow one = bmsFollowService.getOne(
                new LambdaQueryWrapper<BmsFollow>()
                        .eq( BmsFollow::getParentId, parentId )
                        .eq( BmsFollow::getFollowerId, umsUser.getId() ) );
        if (ObjectUtils.isEmpty( one )) {
            ApiAsserts.fail( "未关注！" );
        }
        bmsFollowService.remove( new LambdaQueryWrapper<BmsFollow>().eq( BmsFollow::getParentId, parentId )
                .eq( BmsFollow::getFollowerId, umsUser.getId() ) );
        return ApiResult.success( null, "成功取消关注" );
    }

    @ApiOperation( "验证是否关注" )
    @GetMapping("/validate/{postUserId}")
    public ApiResult<Map<String, Object>> isFollow(@RequestHeader(value = USER_NAME) String userName
            , @PathVariable("postUserId") String postUserId) {

        UmsUser umsUser = umsUserService.getOne( new LambdaQueryWrapper<UmsUser>().eq( UmsUser::getUsername,
                userName ) );

        Map<String, Object> map = new HashMap<>( 16 );
        map.put( "hasFollow", false );
        if (!ObjectUtils.isEmpty( umsUser )) {
            BmsFollow one = bmsFollowService.getOne( new LambdaQueryWrapper<BmsFollow>()
                    .eq( BmsFollow::getParentId, postUserId )
                    .eq( BmsFollow::getFollowerId, umsUser.getId() ) );
            if (!ObjectUtils.isEmpty( one )) {
                map.put( "hasFollow", true );
            }
        }
        return ApiResult.success( map );
    }


}
